package com.stgy.lease.web.admin.controller.attr;

import com.stgy.lease.common.redis.RedisPrefixEnum;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.model.domain.FacilityInfo;
import com.stgy.lease.model.domain.LabelInfo;
import com.stgy.lease.model.enums.FacilityInfoTypeEnum;
import com.stgy.lease.model.enums.LabelInfoTypeEnum;
import com.stgy.lease.web.admin.service.FacilityInfoService;
import com.stgy.lease.web.admin.service.LabelInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:LabelInfoController
 * Package:com.stgy.lease.web.admin.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/8/30 - 11:02
 */
@RestController
@Tag(name = "标签信息管理")
@RequestMapping("/admin/label")
public class LabelInfoController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LabelInfoService labelInfoService;

    private String labelListKey = RedisPrefixEnum.STGY_ADMIN_PREFIX + "label:list";


    /**
     * 使用枚举类接收type
     * 当提交的参数类型固定 有映射的含义时，可以使用枚举类来存储映射
     * springmvc类型转换 接受请求时将数据转为枚举对象，响应时将枚举对象转为类型值
     * 数据库保存数据时，mybatis获取枚举的指定属性值使用
     * springmvc有默认的转换器converter，会把接收的(字符串)参数转为接口要求的类型传入
     * 解决：编写将字符串转为枚举对象的转换器 并注册
     *
     * @param typeEnum
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "[根据类型]查询配套信息列表")
    public Result<List<LabelInfo>> listByEnum(
            @RequestParam(value = "type", required = false) LabelInfoTypeEnum typeEnum) {
        List<LabelInfo> list = null;
        if (typeEnum == null) {
            list = (List<LabelInfo>) redisTemplate.opsForValue().get(labelListKey);
        } else {
            list = (List<LabelInfo>) redisTemplate.opsForValue().get(labelListKey + ":" + typeEnum.getCode());
        }
        if (list == null) {
            list = labelInfoService.listByEnum(typeEnum);
            if (typeEnum == null) {
                redisTemplate.opsForValue().set(labelListKey, list, 1, TimeUnit.DAYS);
            } else {
                redisTemplate.opsForValue().set(labelListKey + ":" + typeEnum.getCode(), list, 1, TimeUnit.DAYS);

            }
        }
        return Result.ok(list);
    }


    //http://localhost:5174/admin/label/saveOrUpdate POST
    //{"id":"","type":1,"name":"安保","icon":"24H安保"}
    @Operation(summary = "新增或者更新标签信息")
    @PostMapping("/saveOrUpdate")
    public Result<String> saveOrUpdate(@RequestBody LabelInfo labelInfo) {
        boolean b = labelInfoService.saveOrUpdateLabelInfo(labelInfo);
        if (b) {
            redisTemplate.delete(labelListKey);
            redisTemplate.delete(labelListKey + ":" + labelInfo.getType().getCode());
        }
        return Result.build(b);
    }

    //http://localhost:5174/admin/label/deleteById?id=null DELETE
    @Operation(summary = "删除标签信息")
    @DeleteMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") List<Long> ids) {
        boolean b = labelInfoService.removeByIds(ids);
        if (b) {
            Set keys = redisTemplate.keys(labelListKey);
            keys.stream().forEach(key -> redisTemplate.delete(key));
        }
        return Result.build(b);
    }
}